//
//  HotSelListCollectionViewController.m
//  GiftItems
//
//  Created by lanou3g on 15/9/20.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "HotSelListCollectionViewController.h"
#import "BlurImageView.h"
#import "HotSelListCollectionViewCell.h"
#import "HotSelListModel.h"
#import "WebViewController.h"
@interface HotSelListCollectionViewController ()
@property(nonatomic, strong) NSMutableArray *dataArray;
@property(nonatomic, strong) NSString *nextUrl;
@property(nonatomic, strong) YYAnimationIndicator *indicator;
@property (nonatomic,strong)UIButton *toTopbutton;
@end

@implementation HotSelListCollectionViewController
static NSString * const reuseIdentifier = @"Cell";

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
// 数据解析
- (void)setUpDataWithUrl:(NSString *)url {
    [LORequestManger GET:url success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        self.nextUrl = dic[@"data"][@"paging"][@"next_url"];
        for (NSDictionary *dictionary in dic[@"data"][@"items"]) {
            HotSelListModel *model = [HotSelListModel shareJsonWithDictionary:dictionary];
            [self.dataArray addObject:model];
        }
        [self.collectionView reloadData];
        [self StopBtn];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setUpDataWithUrl:self.listUrl];
    self.title = self.vcName;
    self.collectionView.backgroundView = [[BlurImageView alloc] initWithFrame:kBounds];
    [self.collectionView registerClass:[HotSelListCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // 上拉加载数据
    __block HotSelListCollectionViewController *hotListVC = self;
    [self.collectionView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        [hotListVC setUpDataWithUrl:hotListVC.nextUrl];
        [hotListVC.collectionView.defaultFooter endRefreshing];
    }];
    // 下拉刷新数据
    [self.collectionView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
        [hotListVC.dataArray removeAllObjects];
        [hotListVC setUpDataWithUrl:hotListVC.listUrl];
        [hotListVC.collectionView reloadData];
        [hotListVC.collectionView.defaultHeader endRefreshing];
    }];
    
    //添加加载动画
    self.indicator = [[YYAnimationIndicator alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-40, self.view.frame.size.height/2-40, 80, 80)];
    [self.indicator setLoadText:@"正在加载..."];
    [self.view addSubview:self.indicator];
    [self startBtn];
    
    [self addToTopButton];
    
    
}
// 回到最顶部
- (void)addToTopButton {
    self.toTopbutton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.toTopbutton.frame = CGRectMake(kWidth - 60, kHeight - 200, 40, 40);
    [self.toTopbutton setImage:[UIImage imageNamed:@"Entypo_e4af(0)_256"] forState:(UIControlStateNormal)];
    [self.toTopbutton addTarget:self action:@selector(toTop) forControlEvents:(UIControlEventTouchUpInside)];
    self.toTopbutton.alpha = 0;
    [self.collectionView addSubview:self.toTopbutton];
}
- (void)toTop {
    
    self.collectionView.contentOffset = CGPointMake(0, 0);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    self.toTopbutton.alpha = self.collectionView.contentOffset.y / 1000.0;
    self.toTopbutton.frame = CGRectMake(kWidth - 60, kHeight- 200 + self.collectionView.contentOffset.y, 40, 40);
}
#pragma mark --- 加载动画 ---
- (void)startBtn {
    // 开始转动
    [self.indicator startAnimation];
}

/*
 当Type为YES时消失  NO时不消失
 */
-(void)StopBtn
{
    [self.indicator stopAnimationWithLoadText:@"finish" withType:YES];//加载成功
}

-(void)StopFailBtn
{
    [self.indicator stopAnimationWithLoadText:@"加载失败" withType:NO];//加载失败
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HotSelListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    HotSelListModel *listModel = self.dataArray[indexPath.row];
    [cell setValueForArrtibutesWithModel:listModel];
    return cell;
}
// cell的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HotSelListModel *hotModel = self.dataArray[indexPath.row];
    WebViewController *webVC = [[WebViewController alloc] init];
    webVC.webUrl = hotModel.purchase_url;
    webVC.nameShare = hotModel.name;
    webVC.imageShare = hotModel.cover_image_url;
    webVC.type = @"更多详情";
    [self presentViewController:webVC animated:YES completion:nil];
}

/*
 // Uncomment this method to specify if the specified item should be highlighted during tracking
 - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
 }
 */

/*
 // Uncomment this method to specify if the specified item should be selected
 - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
 return YES;
 }
 */

/*
 // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
 - (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
 }
 
 - (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
 }
 
 - (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
 }
 */

@end
